package com.flightgame.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Utils {
	public final int SUCCESS = 10;
	public final int OWNERSHIP_AROUND = 11;
	public final int INSUFFICIENT_CREDITS = 12;
	public final int NO_AIRPORT_WITH_CODE = 13;
	public final int INVALID_ID = 14;
	public final int INVALID_USERNAME = 15;
	public final int INVALID_ITEMNAME = 16;
	public final int INVALID_NUMBER_FORMAT = 17;
	public final int NO_OWNERSHIP_WITH_ID = 18;
	public final int NO_USER = 19;
	public final int NOT_AN_AIRCRAFT = 20;
	
	public List<String> tokenizeCommaString(String text){
		List<String> stringList = new ArrayList();
		StringTokenizer tokenizer = new StringTokenizer(text,",");
		while(tokenizer.hasMoreElements()){
			String token = tokenizer.nextToken();
			stringList.add(token);
		}
		return stringList;
	}
	
	public String getParameterValue(String params,String param){
		Utils util = new Utils();
		
		if(params != null){
			List<String> parameters = util.tokenizeCommaString(params);
			
	    	for(String parameter : parameters){
	    		StringTokenizer tokenizer = new StringTokenizer(parameter,"=");
	    		if(tokenizer.nextToken().equals(param)) return tokenizer.nextToken();
	    	}	
		}
		
    	return "";
	}
	
	public String setParameterValue(String params,String param,String value){
		Utils util = new Utils();
		List<String> parameters = util.tokenizeCommaString(params);
		
		for(String parameter : parameters){
    		StringTokenizer tokenizer = new StringTokenizer(parameter,"=");
    		String currParam = tokenizer.nextToken();
    		String currParamValue = tokenizer.nextToken();
    		if(currParam.equals(param)) {
    			return params.replaceFirst(currParam+"="+currParamValue,currParam+"="+value);
    		}
    	}
		
		return params;
	}
	
	public String setDefaultParameters(String typeOfItem){
		if(typeOfItem.equals("aircraft")) return "fuel=100,fuelLimit=100";
		else return "";
	}
	
	public boolean isCoordinatesValid(double lat1,double lat2,double lon1,double lon2){
		return (Math.abs(lat1) < 90 && Math.abs(lat2) < 90 && Math.abs(lon1) < 180 && Math.abs(lon2) < 180) ? true : false;
	}
	
	public String getFileContent(String path){
		
		String content = "";
		String thisLine;
		try {
	          FileInputStream fin =  new FileInputStream(path);

	          BufferedReader myInput = new BufferedReader
	              (new InputStreamReader(fin));
	          while ((thisLine = myInput.readLine()) != null) {  
	             content += thisLine;
	             }
	          }
	       catch (Exception e) {
	         e.printStackTrace();
	         }
	    return content;
	}
	
	public double distanceBetweenCoordinates(double lat1, double lon1, double lat2, double lon2) {
		int R = 6371; // km
		double dLat = (lat2-lat1) * Math.PI / 180;
		double dLon = (lon2-lon1) * Math.PI / 180;
		double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1 * Math.PI / 180 ) * Math.cos(lat2 * Math.PI / 180 ) * Math.sin(dLon/2) * Math.sin(dLon/2);
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
		double d = R * c;

		return d;
	}
	
	public String getMessage(int messageCode){
		switch(messageCode){
			case OWNERSHIP_AROUND : return "There are other ownerships around.";
			case INSUFFICIENT_CREDITS : return "Insufficient Credits.";
			case NO_AIRPORT_WITH_CODE : return "No Airports with the given Airport code.";
			case INVALID_ID : return "Invalid Id parameter(s).";
			case INVALID_USERNAME : return "Invalid username.";
			case INVALID_ITEMNAME : return "Invalid itemname.";
			case INVALID_NUMBER_FORMAT : return "Invalid Number Format.";
			case NO_OWNERSHIP_WITH_ID : return "Invalid Ownership id.";
			case NO_USER : return "User does not exist.";
			case NOT_AN_AIRCRAFT : return "That ownership is not an Aircraft.";
			
		}
		return "";
	}
}
